package com.atguigu.eduservice.Listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.execl.SubjectData;
import com.atguigu.eduservice.service.EduSubjectService;
import com.atguigu.exceptionHandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;


import java.util.Map;

public class SubjectExcelListener extends AnalysisEventListener<SubjectData> {


    //    这个类没有不能给spring管理, 所以需要手动new
    public  EduSubjectService eduSubjectService;


    public SubjectExcelListener(EduSubjectService eduSubjectService) {
        this.eduSubjectService = eduSubjectService;
    }

    public SubjectExcelListener() {
    }

    //判断一级分类是否存在
    public EduSubject exitsOneSubject(String name) {
        //一级分类是title= ? and parent_id =0;
        LambdaQueryWrapper<EduSubject> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EduSubject::getTitle, name);
        wrapper.eq(EduSubject::getParentId, "0");
        EduSubject eduSubjectone = eduSubjectService.getOne(wrapper);
        return eduSubjectone;
    }

    //判断该名字的二级分类是否存在
//    pid
    public EduSubject exitsTwoSubject(String name, String pid) {
        //二级分类是title= ? and parent_id =父节点的id;
        LambdaQueryWrapper<EduSubject> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EduSubject::getTitle, name);
        wrapper.eq(EduSubject::getParentId, pid);
        EduSubject eduSubjecTwo = eduSubjectService.getOne(wrapper);
        return eduSubjecTwo;
    }


    //读取excel表头信息
    //读取表头
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        System.out.println("表头信息：" + headMap);
    }

    //    读取execl内容,一行一行读取数据,第一个值是一级分类,第二个值是二级分类.
    @Override
    public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
        if (subjectData == null) {
            throw new GuliException(20001, "文件数据为空");
        }
//        execl的一级分类和二级分类的名字
        String exitsoneSubjectName = subjectData.getOneSubjectName();
        String exitstwoSubjectName = subjectData.getTwoSubjectName();
//这个是判断一级分类是否重复数据.有的话返回数据,没有的话为null.
        EduSubject eduSubject = this.exitsOneSubject(exitsoneSubjectName);

//            一级分类的id
        String id;
        if (eduSubject == null) {
//            一级分类没有重复数据,可以插入
            EduSubject eduSubject2 = new EduSubject();
            eduSubject2.setParentId("0");
            eduSubject2.setTitle(exitsoneSubjectName);
            eduSubjectService.save(eduSubject2);
//            新的一级分类的id
            id = eduSubject2.getId();
        } else {
            id = eduSubject.getId();
        }

//        一级分类的id,用来查询二级分类是否重复.
        EduSubject eduSubjectTwo = exitsTwoSubject(exitstwoSubjectName, id);
        if (eduSubjectTwo == null) {
//                可以插入,没有重复.
            EduSubject eduSubject1 = new EduSubject();
            eduSubject1.setParentId(id);
            eduSubject1.setTitle(exitstwoSubjectName);
            eduSubjectService.save(eduSubject1);
        }

    }

    //    读取后的操作.
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}